<template>
  <el-button type="primary" :icon="Plus" @click="data.dialogVisible = true">新增宿舍楼</el-button>
  <el-table ref="multipleTableRef" :data="data.tableData" style="width: 100%">
    <el-table-column type="selection" width="55" />
    <el-table-column property="id" label="宿舍楼编号" />
    <el-table-column property="groupName" label="宿舍楼名称" />
  </el-table>
  <el-dialog v-model="data.dialogVisible" title="新增宿舍楼" width="500">
    <el-form>
      <el-form-item label="宿舍楼名称">
        <el-input v-model="data.groupName"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="data.dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="commit">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import axios from "../../api";
import { Plus } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { ref, reactive } from "vue";
// 初始化变量
const data = reactive({
  tableData: [],
  dialogVisible: false,
  groupName: "",
});
// 获取tale数据
const getData = async () => {
  const resp = await axios.get("/roomGroup");
  data.tableData = resp.data.data;
};
// 打开新增弹窗
const commit = () => {
  axios
    .post("/roomGroup", {
      groupName: data.groupName,
    })
    .then((res) => {
      ElMessage.success("宿舍楼新增成功");
      data.dialogVisible = false;
      getData();
    });
};
getData();
</script>

<style lang="less" scoped></style>
